<template>
  <div>
    <el-dialog title="新增行政单位" :visible.sync="dialogVisible" width="500px" :before-close="closeEditDialog"
      :close-on-click-modal="false" :append-to-body="true">
      <el-form :model="addInfo" :rules="rules" ref="refAddInfo" label-width="110px">

        <el-form-item size="small" label="行政区域名称" prop="regionName">
          <el-input clearable v-model.trim="addInfo.regionName" placeholder="请输入行政区域名称" maxlength="15"></el-input>
        </el-form-item>

         <el-form-item size="small" label="行政区域码" prop="regionCode">
          <el-input clearable v-model.trim="addInfo.regionCode" placeholder="请输入行政区域码" maxlength="12" oninput="value=value.replace(/\D/g,'')"></el-input>
        </el-form-item>

         <el-form-item size="small" label="城乡分类代码" v-if="fixedPjtInfo.regionLevel==4|| fixedPjtInfo.regionLevel==3">
          <el-input clearable v-model.trim="addInfo.ruralCode" placeholder="请输入城乡分类代码" maxlength="12" oninput="value=value.replace(/\D/g,'')"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button size="small" class="confirm-button" @click="editSubmit" v-preventReClick="1000">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { handleTree } from "@utils/trans-tree";
import { provinceCityAreaArray } from "@/utils/province-city-area";
import { reqAnalysisRegion } from "@/api/api";
import { mapState } from "vuex";
import treeselect from "@riophae/vue-treeselect"; //树形选择器
import "@components/vue-treeselect.less"; //树形选择器
import { LOAD_CHILDREN_OPTIONS } from "@riophae/vue-treeselect"; //Vue-Treeselect三级动态加载
import asc from "@utils/asc.js"; //加密解密

export default {
  name: "add",
  props: ["fixedPjtInfo"],
  components: { treeselect },
  data() {
    return {
      headers: {
        token: asc.decrypt(sessionStorage.getItem("token")),
      },
      dialogImageUrl: "",
      dialogVisible2: false,
      projectList: [], //单位数据
      provinceCityAreaData: provinceCityAreaArray,
      dialogVisible: false,
      addInfo: {
        regionName:"",
        regionCode:"",
        parentCode:"",
        ruralCode:"",
      },
      // 表单验证规则
      rules: {
        regionName: [
          { required: true, message: "请输入行政区域名称", trigger: "blur" },
        ],
        regionCode: [{ required: true, message: "请输入行政区域码", trigger: "blur" }],
      },
    };
  },
  watch: {},
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    // 点击
    async editSubmit() {
      this.$refs.refAddInfo.validate(async (valid) => {
        if (valid) {
          this.addInfo.parentCode=this.fixedPjtInfo.regionCode;//上级id
          const res = await reqAnalysisRegion(this.addInfo);
          // console.log(res);
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
          this.$modal.notifySuccess("添加行政单位成功");
          this.$parent.Region();
           this.$parent.loadChildren2(this.addInfo.parentCode);
          this.closeEditDialog();
        }
      });
    },
    // 点击关闭弹框
    closeEditDialog() {
      this.addInfo = {
         regionName:"",
        regionCode:"",
        parentCode:"",
        ruralCode:"",
      };
      this.$refs.refAddInfo.resetFields();
      this.dialogVisible = false;
    },
  },
  created() {
  },
};
</script>

<style lang="less" scoped>
@import url("../../common.less");
</style>